﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace UtilsLib.UtilsFile
{
    public class FileObject
    {
        private List<FileRow> _rows;
        private FileRow _columnsName;
        private int _nbColumns;
        private int _nbRows;
        private string _name;

        #region Constructor

        /// <summary>
        /// Empty constructor
        /// </summary>
        public FileObject()
        {
            _rows = new List<FileRow>();
            _columnsName = new FileRow();
            _nbColumns = 0;
            _nbRows = 0;
            _name = "";
        }

        /// <summary>
        /// Set the columns name of the file
        /// </summary>
        /// <param name="columns">The name of the columns</param>
        public FileObject(List<string> columns)
        {
            _columnsName = new FileRow(columns);
            _rows = new List<FileRow>();
            _nbColumns = _columnsName.Columns.Count;
            _nbRows = 0;
            _name = "";
        }

        /// <summary>
        /// Set the rows of the file without the columns name
        /// </summary>
        /// <param name="rows">The values in the rows of the file</param>
        public FileObject(List<List<string>> rows)
        {
            _rows = new List<FileRow>();
            int cpt = 0;
            foreach (List<string> row in rows)
            {
                _rows.Add(new FileRow(row, cpt));
                cpt++;
            }
            _nbColumns = _rows.First().Columns.Count;
            _nbRows = _rows.Count;
            _name = "";
        }
        #endregion Constructor

        /// <summary>
        /// Add a column in the file
        /// </summary>
        /// <param name="colname">The name of the new column</param>
        /// <param name="values">The values in the rows for the new column</param>
        public void AddColumn(string colname, List<string> values)
        {
            ColumnsName.Columns.Add(colname);
            this.AddColumn(values);
        }
        public void AddColumn(FileColumn column, List<FileColumn> values)
        {
            ColumnsName.Columns.Add(column);
            this.AddColumn(values);
        }
        /// <summary>
        /// Add a column in the file
        /// </summary>
        /// <param name="values">The values in hte rows for the new column</param>
        public void AddColumn(List<string> values)
        {
            for (int i = 0; i < this.Rows.Count; i++)
                this.Rows[i].Columns.Add(values[i]);
            this._nbColumns++;
        }
        public void AddColumn(List<FileColumn> values)
        {
            for (int i = 0; i < this.Rows.Count; i++)
                this.Rows[i].Columns.Add(values[i]);
            this._nbColumns++;
        }

        // Attention aux problemes
        /// <summary>
        /// Add a new row in the file
        /// </summary>
        /// <param name="values">The values in the columns for the new row</param>
        public void AddRow(List<string> values)
        {
            this.AddRow(values,this.Rows.Count);
        }
        public void AddRow(List<FileColumn> values)
        {
            this.AddRow(values, this.Rows.Count);
        }
        /// <summary>
        /// Add a new row in the file
        /// </summary>
        /// <param name="values">The values in the columns for the new row</param>
        /// <param name="number">The row number</param>
        public void AddRow(List<string> values, int number)
        {
            //TODO
            this.Rows.Add(new FileRow(values, number));
            
            _nbRows++;

            /*
             * Care about conflicting row number
             * must search and replace / throw ex if conflict or jump the existing
             * row down a number, and every other row below
             * 
             */

        }
        public void AddRow(List<FileColumn> values, int number)
        {
            //TODO
            this.Rows.Add(new FileRow(values, number));

            _nbRows++;

            /*
             * Care about conflicting row number
             * must search and replace / throw ex if conflict or jump the existing
             * row down a number, and every other row below
             * 
             */

        }

        /// <summary>
        /// Get or Set the Rows of the file
        /// </summary>
        public List<FileRow> Rows
        {
            get { return _rows; }
            set { _rows = value; }
        }

        /// <summary>
        /// Get or Set the Columns Name of the file
        /// </summary>
        public FileRow ColumnsName
        {
            get { return _columnsName; }
            set { _columnsName = value; }
        }

        /// <summary>
        /// Get the number of Column
        /// </summary>
        public int NbColumns
        {
            get { return _nbColumns; }
        }
        /// <summary>
        /// Get the number of row
        /// </summary>
        public int NbRows
        {
            get { return _nbRows; }
        }

        /// <summary>
        /// Get or Set the name of the file
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }


    }
}
